package com.yzt.message.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.yzt.message.custom.RXGroupEditContent;
import com.yzt.zhuju.RXingUtils;
import com.yzt.zhuju.base.BaseActivity;
import com.yzt.message.R;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.message.GroupEditBean;
import com.yzt.zhuju.widget.tuiview.TUIEditText;

import io.rong.imkit.RongIM;
import io.rong.imlib.model.Conversation;

/**
 * 作者：bobwang
 * 创建时间：2023-11-19 23:06:16
 * 描述： 群修改昵称
 */
public class GroupEditNameActivity extends BaseActivity {

    private TUIEditText etContent;

    public static void startSelf(Context activity, String groupName, String groupId) {
        activity.startActivity(new Intent(activity, GroupEditNameActivity.class)
                .putExtra("extra_name", groupName)
                .putExtra("extra_groupId", groupId));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_group_edit_name;
    }

    @Override
    public void installViews() {
        ARouter.getInstance().inject(this);
        mTitleBar.setTitle("修改群聊名称");
        etContent = findViewById(R.id.et_content);
        etContent.setText(getIntent().getStringExtra("extra_name"));
        findViewById(R.id.tv_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = etContent.getText().toString().trim();
                if (content.equals("")) {
                    showToast("请输入群聊名称");
                    return;
                }
                BizLayer.getInstance().getMessageModule().groupChatNameEdit(content, getIntent().getStringExtra("extra_groupId"), new AresSubscriber<Object>() {
                    @Override
                    protected void onSuccess(Object bean) {
                        RXGroupEditContent messageContent = new RXGroupEditContent();
                        messageContent.setContent("「修改群名称」");
                        GroupEditBean messageExtra = new GroupEditBean();
                        messageExtra.setGroupName(content);
                        messageExtra.setSendName(RXingUtils.getPersonDetail().getUserBasic().getNickName());
                        messageExtra.setSendUid(RXingUtils.getPersonDetail().getUserBasic().getUserId());
                        messageContent.setMessageExtra(messageExtra);
                        Conversation.ConversationType conversationType = Conversation.ConversationType.GROUP;
                        io.rong.imlib.model.Message message = io.rong.imlib.model.Message.obtain(getIntent().getStringExtra("extra_groupId"),
                                conversationType, messageContent);
                        RongIM.getInstance().sendMessage(message, null, null, null);
                        showToast("修改成功！");
                        finish();
                    }

                    @Override
                    protected void onError(int code, String info) {
                        super.onError(code, info);
                    }

                    @Override
                    protected void onFinallyEnd() {
                        super.onFinallyEnd();
                    }
                });
            }
        });
    }
}
